package com.ygqh.baby.controller.mobile;

import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.FocusType;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.constant.RedisConstant;
import com.ygqh.baby.model.PageModel;
import com.ygqh.baby.model.YgFocusModel;
import com.ygqh.baby.po.YgFileTemplate;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgFileTemplateService;
import com.ygqh.baby.service.YgFocusService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 焦点图
 *
 * @author ZhangSai
 */
@Controller
@RequestMapping("/app/focus")
public class YgFocusController extends BaseController {

	@Autowired
	private YgFocusService ygFocusService;
	@Autowired
	private RedisDao redisDao;
	@Autowired
	private YgFileTemplateService ygFileTemplateService;

	/**
	 * 首页焦点图
	 *
	 * @param pageKey
	 * @return
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public ResponseEntity list(@RequestParam String... pageKey) {
		List<YgFocusModel> resultSet = ygFocusService.searchModelByCache(FocusType.Focus, pageKey);
		return ResponseEntity.success(resultSet);
	}

	/**
	 * 首页信息
	 *
	 * @return
	 */
	@RequestMapping(value = "imglist")
	@ResponseBody
	public ResponseEntity imglist(@RequestParam FileType fileType) {
        String redisKey = String.format(RedisConstant.MALL_TEMPLATE_KEY, fileType.name());
		String result = redisDao.get(redisKey);
		if (StringUtils.isBlank(result)) {
			List<YgFileTemplate> resultSet = ygFileTemplateService.find(fileType);
			if (resultSet.size() == 0) {
				return ResponseEntity.error("系统出现错误");
			}
			result = resultSet.get(0).getImgPath();
			redisDao.set(redisKey, result, 0);
		}
		return ResponseEntity.success(result);
	}


	/**
	 * 分类banner图
	 *
	 * @return
	 */
	@RequestMapping(value = "getCategoryImg")
	@ResponseBody
	public ResponseEntity findCategoryImg() {
		List<YgFocusModel> resultSet = ygFocusService.searchYiJiDianModel(FocusType.Focus);
		List<PageModel> list = formatUrl(resultSet);
		return ResponseEntity.success(list);
	}

	private List<PageModel> formatUrl(List<YgFocusModel> resultSet) {
		List<PageModel> list = new ArrayList<PageModel>();
		for (YgFocusModel d : resultSet) {
			PageModel p = new PageModel();
			if (d.getLinkUrl().indexOf("list.html") > -1) {//列表页
				p.setImagePath(d.getImagePath());
				p.setImageType("List");
				p.setLinkUrl(d.getLinkUrl());
				p.setSearchKey(d.getLinkUrl().substring(d.getLinkUrl().indexOf("?") + 1));
			}
			if (d.getLinkUrl().indexOf("product.html") > -1) {//单品页
				p.setImagePath(d.getImagePath());
				p.setImageType("Detail");
				p.setLinkUrl(d.getLinkUrl());
				String parm = d.getLinkUrl().substring(d.getLinkUrl().indexOf("?"));
				p.setSearchKey(parm.split("&")[0].split("=")[1]);
			}
			if (d.getLinkUrl().indexOf("static") > -1) {//专题
				p.setImagePath(d.getImagePath());
				p.setImageType("WebView");
				p.setLinkUrl(d.getLinkUrl());
				p.setSearchKey("");
			}
			if (d.getLinkUrl().indexOf("group") > -1) {//专题
				p.setImagePath(d.getImagePath());
				p.setImageType("Group");
				p.setLinkUrl(d.getLinkUrl());
				p.setSearchKey("");
			}
			if (d.getLinkUrl().indexOf("seconds") > -1) {//专题
				p.setImagePath(d.getImagePath());
				p.setImageType("Seconds");
				p.setLinkUrl(d.getLinkUrl());
				p.setSearchKey("");
			}
			p.setPageKey(d.getPageKey());
			list.add(p);
		}
		return list;
	}

	@RequestMapping(value = "getAdImgAndExpress")
	@ResponseBody
	public ResponseEntity getAdImgAndExpress() {
		Map<String, Object> map = new HashMap<>();
		map.put("adImg", redisDao.get("YjStartPage"));
		map.put("express", redisDao.get(String.format(RedisConstant.EXPRESS_KEY, SALES_PLATFORM)));
		return ResponseEntity.success(map);
	}

	/**
	 * 获取首页tab 标签内容
	 *
	 * @return
	 */
	@RequestMapping("findHomeTab")
	@ResponseBody
	public ResponseEntity findHomeTab() {
		String key = String.format(RedisConstant.MALL_INDEX_TAB_KEY, SALES_PLATFORM);
		return ResponseEntity.success(redisDao.get(key));
	}
}
